rm(list=ls())
library(tidyverse)
library(haven)

# Read Race ----

race_mun <- read_rds(here::here("data","processed","citycharacteristics","race_mun.rds")) %>% 
  select(-mun_name)

# Read Urban ----

urban_mun <- read_rds(here::here("data","processed","citycharacteristics","urban_mun.rds")) %>% 
  select(-mun_name)

# Read PEA ----

pea_mun <- read_rds(here::here("data","processed","citycharacteristics","pea_mun.rds")) %>% 
  rename(econ_active_workforce = pea_perc) %>% 
  select(-mun_name)

# Read POP ----

pop_mun <- read_rds(here::here("data","processed","citycharacteristics","pop_mun.rds")) %>% 
  filter(year %in% c(2000,2010)) %>% 
  mutate(census_year = as.numeric(year)) %>% 
  select(-mun_name)

# Read Earnings ----
earnings <- read_rds(here::here("data","processed","citycharacteristics","earnings_mun.rds")) %>% 
  rename(census_year = year) %>% 
  select(-mun_name)

# Read Educ ----
educ <- read_rds(here::here("data","processed","citycharacteristics","educ_mun.rds")) 

# Join

demographic_dataset <- race_mun %>% 
  left_join(pea_mun, by = c("mun_code","census_year")) %>% 
  left_join(urban_mun, by = c("mun_code","census_year")) %>% 
  left_join(pop_mun, by = c("mun_code","census_year")) %>% 
  left_join(earnings, by = c("mun_code","census_year")) %>% 
  left_join(educ, by = c("mun_code","census_year")) %>% 
  mutate(mun_code = as.numeric(mun_code)) %>% 
  filter(census_year %in% c(2000,2010))

# Duplicate census 2000 data to be used for 2004 and 2008 election
demographic_dataset_2004 <- demographic_dataset %>% 
  filter(census_year == 2000) %>% 
  mutate(year = 2004)

demographic_dataset_2008 <- demographic_dataset %>% 
  filter(census_year == 2000) %>% 
  mutate(year = 2008)

demographic_dataset_2012 <- demographic_dataset %>% 
  filter(census_year == 2010) %>% 
  mutate(year = 2012)

demographic_dataset_complete <- bind_rows(demographic_dataset_2004,
                                          demographic_dataset_2008,
                                          demographic_dataset_2012)

write_dta(demographic_dataset_complete, here::here("data","processed","demographic_dataset.dta"))
